﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaoJianSoft.Common.Properties;

namespace BaoJianSoft.Common.Globalization
{
	/// <summary>
	/// Globalization convert methods.
	/// </summary>
	public static class LocalizedDateTime
	{
        private static readonly DefaultDateTimeLocalizer defaultDateTimeLocalizer = new DefaultDateTimeLocalizer();

		/// <summary>
		/// Convert input date to string without time segment.
		/// </summary>
		/// <param name="dateTime"></param>
		/// <param name="emptyStringWhenEarlierEqualThanDate">Returns empty string when specified date is earlier/equal than this argument.</param>
		/// <returns></returns>
		public static string ToDateString(DateTime dateTime, DateTime emptyStringWhenEarlierEqualThanDate)
		{
			if (dateTime <= emptyStringWhenEarlierEqualThanDate) return "";
            return ToDateString(dateTime);
		}

		/// <summary>
		/// Convert input date to string without time segment.
		/// </summary>
		/// <param name="dateTime"></param>
		/// <returns></returns>
		public static string ToDateString(DateTime dateTime)
		{
            IDateTimeLocalizer localizer = SpringContext.Current.GetObject<IDateTimeLocalizer>();
            if (localizer == null)
                return defaultDateTimeLocalizer.ToDateString(dateTime);
            else
                return localizer.ToDateString(dateTime);
		}

		/// <summary>
		/// Convert input date/time to string
		/// </summary>
		/// <param name="dateTime"></param>
		/// <param name="emptyStringWhenEarlierEqualThanDate">Returns empty string when specified date is earlier/equal than this argument.</param>
		/// <returns></returns>
		public static string ToDateTimeString(DateTime dateTime, DateTime emptyStringWhenEarlierEqualThanDate)
		{
			if (dateTime <= emptyStringWhenEarlierEqualThanDate) return "";
            return ToDateTimeString(dateTime);
		}

		/// <summary>
		/// Convert input date/time to string
		/// </summary>
		/// <param name="dateTime"></param>
		/// <returns></returns>
		public static string ToDateTimeString(DateTime dateTime)
		{
            IDateTimeLocalizer localizer = SpringContext.Current.GetObject<IDateTimeLocalizer>();
            if (localizer == null)
                return defaultDateTimeLocalizer.ToDateTimeString(dateTime);
            else
                return localizer.ToDateTimeString(dateTime);
		}
	}
}